﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace exsportance.Silverlight.Validate
{
    /// <summary>
    /// A list of messages from validators
    /// </summary>
    public class ValidationMessages : List<ValidationMessage>
    {
        /// <summary>
        /// Adds a new message
        /// </summary>
        /// <param name="text"></param>
        /// <param name="relatedControl">The control the text belongs to</param>
        /// <returns></returns>
        public ValidationMessage Add(string text, FrameworkElement relatedControl)
        {
            ValidationMessage message = new ValidationMessage(text, relatedControl);
            Add(message);

            return message;
        }

        /// <summary>
        /// Add new messages
        /// </summary>
        /// <param name="messages">Message list</param>
        /// <param name="control">The control the text belongs to</param>
        public void AddRange(List<string> messages, FrameworkElement control)
        {
            foreach (string message in messages)
                Add(message, control);
        }

        /// <summary>
        /// Get all messages as string
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            foreach (ValidationMessage message in this)
                sb.AppendLine(message.ToString());

            return sb.ToString();
        }

        /// <summary>
        /// Get all messages related to a control
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public ValidationMessages GetByControl(FrameworkElement control)
        {
            ValidationMessages result = new ValidationMessages();

            // add messages
            result.AddRange(this.Where(m => m.Control == control));

            return result;
        }


    }

    /// <summary>
    /// A message from a validation process
    /// </summary>
    public class ValidationMessage
    {
        /// <summary>
        /// The messages
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// The control the message belongs to
        /// </summary>
        public FrameworkElement Control { get; private set; }

        #region constructor
        /// <summary>
        /// Create a new ValidationMessage
        /// </summary>
        public ValidationMessage()
        {

        }

        /// <summary>
        /// Create a new ValidationMessage
        /// </summary>
        /// <param name="text"></param>
        /// <param name="relatedControl"></param>
        public ValidationMessage(string text, FrameworkElement relatedControl)
            : this()
        {
            Text = text;
            Control = relatedControl;
        }
        #endregion

        public override string ToString()
        {
            return Text;
        }
    }
}
